package cn.tedu_03;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class Test3 {
    public static void main(String[] args) throws InstantiationException, IllegalAccessException, NoSuchFieldException, InvocationTargetException {

        // method();
        // method2();
        // method3();
        // method4();
        //method5();
        //method6();
        //method7();
        //method8();
        // method9();


    }

    private static void method9() throws IllegalAccessException, InstantiationException, InvocationTargetException {
        Class c = Human.class;
        Method [] a=c.getDeclaredMethods();
        for (Method m : a) {
            Object obj=c.newInstance();

            if(m.getName().equals("test1")){
                m.setAccessible(true);
                m.invoke(obj);
            }
            if(m.getName().equals("test2")){
                m.setAccessible(true);
                m.invoke(obj,"jack");
            }

        }

    }

    private static void method8() throws IllegalAccessException, InstantiationException, InvocationTargetException {
        Class c =Human.class;
        Method [] a=c.getDeclaredMethods();
        for (Method m : a) {
            Object obj =c.newInstance();

            if(m.getName().equals("test1")){
                m.setAccessible(true);
                m.invoke(obj);
            }
            if(m.getName().equals("test2")){
                m.setAccessible(true);
                m.invoke(obj,"nihao");
            }
        }

    }

    private static void method7() throws IllegalAccessException, InstantiationException {
        Class c = Human.class;
        Field [] a =c.getDeclaredFields();
        for (Field f : a) {
            Object obj=c.newInstance();

            if(f.getName().equals("name")){
                f.set(obj, "jack");
                System.out.println(f.get(obj));
            }
            if(f.getName().equals("age")){
                f.setAccessible(true);
                f.set(obj,25);
                System.out.println(f.get(obj));

            }

        }

    }

    private static void method6() throws IllegalAccessException, InstantiationException {
        Class c = Human.class;
        Field [] a=c.getDeclaredFields();
        for (Field f : a) {
            Object obj=c.newInstance();
            if(f.getName().equals("name")){
                f.set(obj,"jack");
                System.out.println(f.get(obj));
            }
            if(f.getName().equals("age")){
                f.setAccessible(true);//对private的属性,用setAccessible开启权限才可以
                f.set(obj,20);
                System.out.println(f.get(obj));
            }

        }

    }

    private static void method5() throws IllegalAccessException, InstantiationException, InvocationTargetException {
        Class c =Human.class;
        Constructor[] a=c.getConstructors();
        for (Constructor con : a) {
            Class [] cc=con.getParameterTypes();
            if(cc.length==0){
                Object obj=con.newInstance();
                System.out.println(obj);
            }
            if(cc.length!=0){
                Object obj1= con.newInstance("juon",20,20000);
                System.out.println(obj1);

            }
        }

    }

    private static void method4() throws IllegalAccessException, InvocationTargetException, InstantiationException {
        Class c = Human.class;
        Constructor [] a =c.getConstructors();
        for (Constructor con : a) {
            Class [] cc=con.getParameterTypes();
            if(cc.length==0){
                Object o= con.newInstance();
                System.out.println(o);
            }
            if(cc.length!=0){
                Object o1= con.newInstance("jack",20,10000);
                System.out.println(o1);
            }

        }

    }

    private static void method3() throws IllegalAccessException, InstantiationException, InvocationTargetException {
        Class c= Human.class;
        Method[] a =c.getMethods();
        for (Method m : a) {
            System.out.println(m.getName());
            if(m.getName().equals("test1")){
                Object obj=c.newInstance();
                m.invoke(obj);
            }
        }

    }

    private static void method2() throws IllegalAccessException, InstantiationException {
        Class c =Human.class;
        Field [] a=c.getFields();
        for (Field f : a) {
            //  System.out.println(a);


            if(f.getName().equals("name")){
                Object obj =c.newInstance();
                f.set(obj,"jack");
                System.out.println(f.get(obj));
            }
        }


    }

    private static void method() throws IllegalAccessException, InstantiationException, NoSuchFieldException {
        Class c = Human.class;
        Field[] a = c.getFields();
        for (Field f : a) {
            System.out.println(f.getName());

            if (f.getName().equals("name")) {
                Object obj = c.newInstance();
                f.set(obj, "jack");
                System.out.println(f.get(obj));

            }

            if(f.getName().equals("age")){
                Object obj=c.newInstance();
                f.set(obj,20);
                System.out.println(f.get(obj));
            }


//        Field f=c.getField("age");
//
//            Object obj =c.newInstance();
//            f.set(obj,20);
//            System.out.println(f.get(obj));
        }
    }
}

